package org.zjkoye.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zjkoye.crm.common.PageResultBean;
import org.zjkoye.crm.common.Result;
import org.zjkoye.crm.entity.Region;
import org.zjkoye.crm.service.RegionService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zjkoye
 * @since 2024-06-23
 */
@RestController
@RequestMapping("/region")
public class RegionController {

    private static final Logger logger = LoggerFactory.getLogger(RegionController.class);

    @Autowired
    private RegionService regionService;

    @GetMapping("/pageList")
    @Operation(summary = "地区列表分页查询")
    @Parameters({
            @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token"),
            @Parameter(name = "pageSize", required = true, description = "每页显示的条数"),
            @Parameter(name = "currentPage", required = true, description = "要查询的页"),
            @Parameter(name = "name", description = "地区名称", required = false)
    })
    public Result<PageResultBean<Region>> pageList(@RequestParam int pageSize, @RequestParam int currentPage, @RequestParam(required = false) String name) {
        IPage<Region> page = regionService.pageList(pageSize, currentPage, name);
        if (page == null) {
            return Result.error("查询失败");
        }
        PageResultBean<Region> pageResultBean = new PageResultBean<>(page.getTotal(), page.getRecords());
        return Result.success(pageResultBean);
    }

    @PostMapping("/add")
    @Operation(summary = "添加地区")
    @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token")
    public Result<String> addRegion(@RequestBody Region region) {
        boolean result = regionService.save(region);
        if (!result) {
            return Result.error("添加失败");
        }
        return Result.success("添加成功");
    }

    @PutMapping("/update")
    @Operation(summary = "修改地区信息")
    @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token")
    public Result<String> updateRegion(@RequestBody Region region) {
        boolean result = regionService.updateById(region);
        if (!result) {
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除地区信息")
    @Parameters({
            @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token"),
            @Parameter(name = "id", required = true, description = "地区id")
    })
    public Result<String> deleteRegion(@PathVariable int id) {
        boolean result = regionService.removeById(id);
        if (!result) {
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }

    @GetMapping("/getById")
    @Operation(summary = "根据id查询地区信息")
    @Parameters({
            @Parameter(name = "Authorization", in = ParameterIn.HEADER, required = true, description = "token"),
            @Parameter(name = "id", required = true, description = "地区id")
    })
    public Result<Region> getRegionById(@RequestParam int id) {
        Region region = regionService.getById(id);
        if (region == null) {
            return Result.error("获取地区信息失败");
        }
        return Result.success(region);
    }
}
